/*
 * @Author: fengweicong 75202172+fengweicong@users.noreply.github.com
 * @Date: 2022-07-09 11:46:12
 * @LastEditors: fengweicong 75202172+fengweicong@users.noreply.github.com
 * @LastEditTime: 2022-07-15 10:57:26
 * @FilePath: \B2B\ebuy\src\utils\utils.js
 * @Description: 
 * 
 * Copyright (c) 2022 by fengweicong 75202172+fengweicong@users.noreply.github.com, All Rights Reserved. 
 */
import { regionData, CodeToText, TextToCode } from "element-china-area-data";
export function AreaData() { return regionData }

// 地区代码转文本 areaCode:[] return '***/***/***'
export function AreaCodeToText(areaCode) {
  if (areaCode.constructor !== Array || areaCode.length == 0) return areaCode
  let ans = "", len = areaCode.length;
  for (let i = 0; i < len; i++) {
  ans += CodeToText[areaCode[i]];
  if (i + 1 < len) ans += '/'
  }
  return ans
}

// 地区文本转代码 areaText:'***/***/***' return []
export function AreaTextToCode(areaText) {
  if (!areaText) return []
  let area = areaText.split('/')
  let arr = TextToCode
  let ans = []
  for (let i = 0; i < area.length; i++) {
    arr = arr[area[i]]
    ans.push(arr.code)
  }
  return ans
}

// 上传图片
const COS = require("cos-js-sdk-v5");
export function upLoadPicture(res, callback) {
  let cos = new COS({
    SecretId: "AKIDWu3vWszpOUP4WhdIgnfS6XkiQs66qJXV",
    SecretKey: "kFVXJtDJAn9E3brqb6KHyNVMwGJ5aolQ",
  });
  cos.putObject(
    {
      Bucket: "share-picture-1311771308" /* 必须:存储桶 */,
      Region: "ap-guangzhou" /* 存储桶所在地域，必须字段 */,
      Key: res.file.name /* 必须 :文件名 */,
      StorageClass: "STANDARD", // 上传模式
      Body: res.file, // 上传文件对象
      onProgress: function (progressData) {
        // 进度条
        // console.log(JSON.stringify(progressData));
      },
    },
    (err, data) => {
      // 注意用箭头函数,否则会出现this的指向问题
      console.log(err || data);
      if (data) {
        // 上传成功
        callback(`http://${data.Location}`)
        // this.pictureList.push(this.imageUrl);
        // console.log(this.pictureList);
      }
    }
  );
}

// 输入校验的规则
export const Rules = {
  phone: (rule, value, callback) => {
    if (this.inputSupplierLinkPhone == "") {
      callback(new Error("请输入手机号"));
    } else {
      if (/^1[23456789]\d{9}$/.test(this.inputSupplierLinkPhone)) {
        callback();
      } else {
        callback(new Error("手机号格式错误"));
      }
    }
  },
}